<template>
  <div></div>
</template>

<script>
export default {
  data() {
    return {
      code: this.$route.query.code,
    };
  },
  methods: {
    async getUnionId() {
      return new Promise((resolve) => {
        this.$axios({
          method: "get",
          url: "/wechat_api/sns/oauth2/access_token",
          params: {
            appid: "wx7870d5e16708e05e",
            secret: "5b3febb92f2ea96bb8348c45bb0e5b83",
            code: this.code,
            grant_type: "authorization_code",
          },
        }).then((res) => {
          resolve(res.data.unionid);
        });
      });
    },
    wxLogin() {
      localStorage.removeItem("Authorization");
      this.getUnionId().then((unionid) => {
        this.$axios({
          method: "post",
          url: "/api/adminapi/signin",
          data: {
            signin_type: "wx",
            key: this.$AES.encrypt(unionid),
          },
        }).then((res) => {
          // 解密用户类型和激活状态
          if (res.data.ret == 0) {
            let usertype = res.data.usertypen;
            let is_active = res.data.is_active;
            let username = res.data.username;
            let token = res.data.token;
            let mode=res.data.user_mode;
            let userid=res.data.userid;
            // 相关信息放入缓存
            this.$store.dispatch("login", {
              username,
              token,
              usertype,
              is_active,
              unionid,
              mode,
              userid
            });
            let usertype_int = parseInt(this.$AES.decrypt(usertype));
            if (usertype_int >= 0) {
              this.$router.push("/mgr/communitymanagement");
              this.$message.success("登录成功");
            }
            // 登录失败
          } else if (res.data.ret == 1 || res.data.ret == 3) {
            this.$message.error(res.data.msg);
          }
        });
      });
    },
  },
  mounted() {
    this.wxLogin();
  },
};
</script>

<style scoped>
</style>